package com.projet.whattowatch.adapter;

import java.util.ArrayList;

import com.projet.whattowatch.R;
import com.projet.whattowatch.activity.ConnectedActivity;
import com.projet.whattowatch.activity.MainActivity;
import com.projet.whattowatch.activity.PostGenreActivity;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
 
public class GenreAdapterPlus extends ArrayAdapter<String>
{
	private final Activity context;
	private final ArrayList<String> name;
	
	final String EXTRA_MOVIE = "movie_name";
	final String EXTRA_MOVIE_URL = "movie_url";
	
	public GenreAdapterPlus(Activity context, ArrayList<String> name)
	{
		super(context, R.layout.custom_listview_genre_plus, name);
		this.context = context;
		this.name = name;
	}
	
	@Override
	public int getCount()
	{
        return name.size();
    }
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent)
	{
		LayoutInflater inflater = context.getLayoutInflater();
		View rootView = inflater.inflate(R.layout.custom_listview_genre_plus, null);

		TextView nameGenre = (TextView) rootView.findViewById(R.id.genre);
		Button addButton = (Button) rootView.findViewById(R.id.addButton);
		nameGenre.setText(name.get(position));

		
		addButton.setOnClickListener(new OnClickListener()
		{
            @Override
            public void onClick(View v)
            {
    			ConnectedActivity.mUtilisateur.addGenreFav(name.get(position));
    			MainActivity.app_db.addGenreFav(ConnectedActivity.mUtilisateur, name.get(position));
    			Toast.makeText(context, R.string.add_genres_fav, Toast.LENGTH_SHORT).show();
            }
        });
		
		rootView.setOnClickListener(new OnClickListener()
		{
            @Override
            public void onClick(View v)
            {
            	ArrayList<String> movieName = new ArrayList<String>();
	            ArrayList<String> movieUrl = new ArrayList<String>();
	            
	            for(int i = 0; i < MainActivity.mLibFilm.getListFilm().size(); i++)
	            {
	            	ArrayList<String> genreList = MainActivity.mLibFilm.getFilm(i).getListGenre();
	            	for(int j = 0; j < genreList.size(); j++)
	            	{
	            		if(genreList.get(j).equals(name.get(position)))
	            		{
	            			movieName.add(MainActivity.mLibFilm.getFilm(i).getNom());
	            			movieUrl.add(MainActivity.mLibFilm.getFilm(i).getImageUrl());
	            		}
	            	}
	            }
		    	
		    	Intent listFilm = new Intent(context, PostGenreActivity.class);
		    	listFilm.putStringArrayListExtra(EXTRA_MOVIE, movieName);
		    	listFilm.putStringArrayListExtra(EXTRA_MOVIE_URL, movieUrl);
		    	context.startActivity(listFilm);
            }
        });
		
		return rootView;
	}
}